home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Utilities / Converters / Convert_MacPaint / Source / shared.subproj / PSFile.m < prev    next >
Text File  |  1995-06-12  |  11KB  |  261 lines

  1. /***********************************************************************\
  2. Common class for doing some rudimentarly interactions with ps files in all Convert programs
  3. Copyright (C) 1993 David John Burrowes
  4.  
  5. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version.
  6.  
  7. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
  8.  
  9. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  10.  
  11. The author, David John Burrowes, can be reached at:
  12.     davidjohn@kira.net.netcom.com
  13.     David John Burrowes
  14.     1926 Ivy #10
  15.     San Mateo, CA 94403-1367
  16. \***********************************************************************/
  17.  
  18. #import "PSFile.h"
  19. #import<stdio.h>
  20. #import <stdlib.h>
  21. #import <string.h>
  22. #include <stdarg.h>    // For variable arguments stuff
  23.  
  24. @implementation PSFile
  25.  
  26. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  27. //    Method:    writeDCDcomment:
  28. //    Parameters:    the line of text we are to write as a comment
  29. //    Returns:     a Reply
  30. //    Description:
  31. //        This writes the specified string to the PS file, following it with a newline, and
  32. //        preceeding it immediately with a %% so it appears as a PS DCD comment
  33. //        It does no formatting of the line, as some other smarter routine should.
  34. //    Note:
  35. //        This blindly assumes that it first thing on a new line.  A good PS file object
  36. //        should be keeping track of these things, and jump to a new line if we aren't.
  37. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  38. - WriteDSCComment: (CString) comment
  39. {
  40.     CString    output = NewCString(strlen(comment) + 4);
  41.  
  42.     sprintf(output, "%%%%%s\n", comment);    //That's, two percents and a string. =)
  43.     [self Write: strlen(output) BytesFrom: (ByteString) output];
  44.     FreeCString(output);
  45.     return self;
  46. }
  47.  
  48.  
  49. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  50. //    Method:        WriteDSCCommentUsing:WithFormat:
  51. //    Parameters:    A buffer provided by the caller, a format, and N arguments to be written
  52. //    Returns:     self
  53. //    Stores:        nothing (bug)
  54. //    Description:
  55. //        This writes out a text string given variable arguments, as a DSC comment.
  56. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  57. - WriteDSCCommentUsing: (CString) buffer WithFormat: (CString) format, ...;
  58. {
  59.     va_list parameter_list;
  60.     CString    output;
  61.  
  62.     va_start(parameter_list, format);
  63.  
  64.     vsprintf(buffer, format, parameter_list);     // doc implies this does a va_end
  65.  
  66.     output = NewCString(strlen(buffer) + 4);
  67.     sprintf(output, "%%%%%s\n", buffer);    //That's, two percents and a string. =)
  68.     [self Write: strlen(output) BytesFrom: (ByteString) output];
  69.     FreeCString(output);
  70.     return self;
  71. }
  72.  
  73.  
  74. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  75. //    Method:    WriteComment:
  76. //    Parameters:    the line of text we are to write as a comment
  77. //    Returns:     a Reply
  78. //    Description:
  79. //        This writes the specified string to the PS file, following it with a newline, and
  80. //        preceeding it immediately with a %.
  81. //        It does no formatting of the line, as some other smarter routine should.
  82. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  83. - WriteComment: (CString) comment
  84. {
  85.     CString    output = NewCString(strlen(comment) + 3);
  86.  
  87.     sprintf(output, "%%%s\n", comment);    //That's, one percent and a string. =)
  88.     [self Write: strlen(output) BytesFrom: (ByteString) output];
  89.     FreeCString(output);
  90.     return self;
  91. }
  92.  
  93.  
  94. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  95. //    Method:        WriteCommentUsing:WithFormat:
  96. //    Parameters:    A buffer provided by the caller, a format, and N arguments to be written
  97. //    Returns:     self
  98. //    Stores:        nothing (bug)
  99. //    Description:
  100. //        This writes out a text string given variable arguments, as a PS comment.
  101. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  102. - WriteCommentUsing: (CString) buffer WithFormat: (CString) format, ...;
  103. {
  104.     va_list parameter_list;
  105.     CString    output;
  106.     
  107.     va_start(parameter_list, format);
  108.  
  109.     vsprintf(buffer, format, parameter_list);      // doc implies this does a va_end
  110.  
  111.     output = NewCString(strlen(buffer) + 2);
  112.     sprintf(output, "%%%s\n", buffer);    //That's, 1 percent and a string.
  113.     [self Write: strlen(output) BytesFrom: (ByteString) output];
  114.     FreeCString(output);
  115.     return self;
  116. }
  117.  
  118.  
  119.  
  120.  
  121. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  122. //    Method:    writePSline:
  123. //    Parameters:    the line of text we are to write
  124. //    Returns:     a Reply
  125. //    Description:
  126. //        This writes the specified string to the PS file, following it with a newline.
  127. //        It does no formatting of the line, as some other smarter routine should.
  128. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  129. - WritePSLine: (CString) theLine
  130. {
  131.     CString    output = NewCString(strlen(theLine) + 2);
  132.  
  133.     sprintf(output, "%s\n", theLine);
  134.     [self Write: strlen(output) BytesFrom: (ByteString) output];
  135.     FreeCString(output);
  136.     return self;
  137. }
  138.  
  139.  
  140. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  141. //    Method:        WritePSLineUsing:WithFormat:
  142. //    Parameters:    A buffer provided by the caller, a format, and N arguments to be written
  143. //    Returns:     self
  144. //    Stores:        nothing (bug)
  145. //    Description:
  146. //        This writes out a text string given variable arguments
  147. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  148. - WritePSLineUsing: (CString) buffer WithFormat: (CString) format, ...;
  149. {
  150.     va_list parameter_list;
  151.     CString    output;
  152.     
  153.     va_start(parameter_list, format);
  154.  
  155.     vsprintf(buffer, format, parameter_list);      // doc implies this does a va_end
  156.  
  157.     output = NewCString(strlen(buffer) + 1);
  158.     sprintf(output, "%s\n", buffer);
  159.     [self Write: strlen(output) BytesFrom: (ByteString) output];
  160.     FreeCString(output);
  161.     return self;
  162. }
  163.  
  164.  
  165. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  166. //    Method:    write:BytesOfHexData:
  167. //    Parameters:    The number of Bytes to write
  168. //                A buffer of binary data
  169. //    Returns:     self
  170. //    Description:
  171. //        This method writes out num Bytes of data from the buffer specified by buffer.
  172. //        buffer is not modified.  Each Byte is converted to a pair of hexidcecimal digits,
  173. //        and these are written out.  When the whole buffer has been written, a newline
  174. //        is added.
  175. //        The intended use for this method is for dumping raw image data to be used
  176. //        with PS operators like (surprisingly) image.
  177. //    History:
  178. //        93.08.21    djb    rewrote so that this did all it's hex conversion itself rather than
  179. //                    calling WriteByteAsHex over and over... it seemed to be that that
  180. //                    was almost certainly an enormous performance drain.
  181. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  182. - Write: (PositiveInteger) num BytesOfHexDataFrom: (ByteString) buffer
  183. {
  184.     PositiveInteger    index;
  185.     CString            dest        = NewCString((num*2 ) + 1);
  186.     Byte            nibble1;
  187.     Byte            nibble2;
  188.  
  189.     for (index = 0; index < num; index++)
  190.     {
  191.         nibble1 = buffer[index] >> 4;
  192.         nibble2 = ( buffer[index] & 0x0F);
  193.  
  194.         if (nibble1 < 10)
  195.             dest[index*2] =  (Character) '0'+nibble1; // digit 0-9
  196.         else
  197.             dest[index*2] =  (Character) 'A'+nibble1-10; // cap A-F
  198.     
  199.         if (nibble2 < 10)
  200.             dest[(index*2) + 1] =   (Character) '0'+nibble2; // digit 0-9
  201.         else
  202.             dest[(index*2)+1] = (Character) 'A'+nibble2-10; // cap A-F
  203.     }
  204.     dest[index*2] = EndOfCString;
  205.     
  206.     [self   WriteTextLine: dest];
  207.     FreeCString(dest);
  208.  
  209.     return self;
  210. }
  211.  
  212.  
  213. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  214. //    Method:    write:InvertedBytesOfHexDataFrom:
  215. //    Parameters:    The number of Bytes to write
  216. //                A buffer of binary data
  217. //    Returns:     self
  218. //    Description:
  219. //        This method writes out num Bytes of data from the buffer specified by buffer.
  220. //        buffer is not modified.  Each Byte is converted to a pair of hexidcecimal digits,
  221. //        and these are written out with a newline following all.
  222. //        During the process, the Byte is inverted (thus 00 becomes FF, etc.
  223. //        The intended use for this method is for dumping raw image data to be used
  224. //        with PS operators like (surprisingly) image.
  225. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  226. - Write: (PositiveInteger) num InvertedBytesOfHexDataFrom: (ByteString) buffer
  227. {
  228.     PositiveInteger index;
  229.     for (index = 0; index < num; index++)
  230.         [self    WriteByteAsHex: ~buffer[index]];
  231.     [self ForceNewLine];
  232.     return self;
  233. }
  234.  
  235.  
  236. - ForceNewLine
  237. {
  238.     return [self WriteByte: (Byte) '\n'];
  239.     return self;
  240. }
  241.  
  242.  
  243. - WriteByteAsHex: (Byte) theByte
  244. {
  245.     Byte        nibble1 = theByte >> 4;
  246.     Byte        nibble2 = (theByte & 0x0F);
  247.         
  248.     if (nibble1 < 10)
  249.         [self WriteByte: (Byte) '0'+nibble1]; // digit 0-9
  250.     else
  251.         [self WriteByte: (Byte) 'A'+nibble1-10]; // cap A-F
  252.  
  253.     if (nibble2 < 10)
  254.         [self WriteByte: (Byte) '0'+nibble2]; // digit 0-9
  255.     else
  256.         [self WriteByte: (Byte) 'A'+nibble2-10]; // cap A-F
  257.     return self;
  258. }
  259.  
  260.  
  261. @end